package com.apply.leetcode.puzzle203;

/**
 * 【使用递归】
 * 删除链表中的节点
 */
public class Solution3 {

    public ListNode removeElements(ListNode head, int val) {
        if (head == null) {
            return null;
        }
        // 写法一：
        /*ListNode res = removeElements(head.next, val);
        if (head.val == val) {
            return res;
        }else {
            head.next = res;
            return head;
        }*/

        // 写法二：精简高逼格版
        head.next = removeElements(head.next, val);
        return head.val == val ? head.next: head;

    }

    public static void main(String[] args) {
        // 把数组转化为链表并打印
        int[] nums = {1,2,6,3,4,5,6};
        ListNode head = new ListNode(nums);
        System.out.println(head);

        // 删除全部指定值的节点
        Solution3 solution3 = new Solution3();
        ListNode res = solution3.removeElements(head, 6);
        System.out.println(res);
    }
}
